// locutor
// (c) 2009, Normen Müller, normen.mueller@gmail.com  
// $Id: RegistrySpecInsert.scala 74 2010-05-07 21:01:48Z normen.mueller $
package locutor.spec.registry

import org.specs._

import scaup.Views._
import scala.xml.XML
import locutor.svn.reg._
import locutor.spec.Util._

object RegistrySpecInsert extends Specification("Insert") {
  def throwWarnings = addToSusVerb("throw warnings due to")  
  "Insert" should throwWarnings {    
    "working copy has already been registered" in {
      resetAllWC()
      setupWC("wca")

      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCA) must throwA[RegistryWarning]
    }
  }
  
  
  def throwExceptions = addToSusVerb("throw exceptions due to")
  "Insert" should throwExceptions {
    "another working copy is registered at same file path" in {
      val r = newRegistry()
      r.insert(WCA)
 
      resetWC("wcb")
      ioMkDir("import/wcb/b")
      svnImport("import", "https://svn.kwarc.info/repos/sandbox", "")
      ioRemove("wca")
      svnCheckout(URLB, "wca")
            
      r.insert(WCA) must throwA[RegistryError]
    }
  }
  
  
  def addWC = addToSusVerb("add a working copy")
  "Insert" should addWC {
    "with all committed externals (1st example)" in {
      setupWC("wca")
      setupWC("wcb")

      var r = newRegistry()
      r.insert(WCB)
      r.serialize()

      XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
           </wc>
         </registry>))
    }
    "with all committed externals (2nd example)" in {
      svnSetExternal("wcb/b", (-1, URLC, "c"))
      svnCommit("wcb", "")

      val r = newRegistry()
      r.insert(WCB)
      r.serialize()

      XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a"/>
             <external xml:id={(WCB/"b/c").path.toMD5} own={(WCB/"b").path} rev="-1" url={URLC} tgt="c"/>
           </wc>
         </registry>))
    }
    "with all committed externals (3rd example)" in {
      svnSetExternal("wca/a", (-1, URLC, "c"))
      svnCommit("wca", "")
      svnUpdate("wcb")

      val r = newRegistry()
      r.insert(WCB)
      r.serialize()

      XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a">
               <external xml:id={(WCB/"a/a/c").path.toMD5} own={(WCB/"a/a").path} rev="-1" url={URLC} tgt="c"/>
             </external>
             <external xml:id={(WCB/"b/c").path.toMD5} own={(WCB/"b").path} rev="-1" url={URLC} tgt="c"/>
           </wc>
         </registry>))
    }
  }
  
  
  def addWCs = addToSusVerb("add working copies")
  "Insert" should addWCs {
    "foreach with all committed externals" in {
      setupWC("wca")
      setupWC("wcb")
      
      svnSetExternal("wca/a", (-1, URLC, "c"))
      svnCommit("wca", "")
      
      svnSetExternal("wcb/b", (-1, URLC, "c"))
      svnCommit("wcb", "")
      
      svnUpdate("wcb")
      
      val r = newRegistry()
      r.insert(WCA)
      r.insert(WCB)
      r.serialize()

      XML.loadFile(REG) must ==/(
        (<registry version="0.1" xmlns="http://locutor.kwarc.info">
           <wc xml:id={WCA.path.toMD5} url={URLA} root={WCA.path}>
             <external xml:id={(WCA/"a/c").path.toMD5} own={(WCA/"a").path} rev="-1" url={URLC} tgt="c"/>
           </wc>
           <wc xml:id={WCB.path.toMD5} url={URLB} root={WCB.path}>
             <external xml:id={(WCB/"a").path.toMD5} own={WCB.path} rev="-1" url={URLA} tgt="a">
               <external xml:id={(WCB/"a/a/c").path.toMD5} own={(WCB/"a/a").path} rev="-1" url={URLC} tgt="c"/>
             </external>
             <external xml:id={(WCB/"b/c").path.toMD5} own={(WCB/"b").path} rev="-1" url={URLC} tgt="c"/>
           </wc>
         </registry>))
    }
  } 
}
